class AjaxTreeGenerator < Rails::Generator::NamedBase

 attr_reader    :controller_name,
                :controller_class_path,
                :controller_file_path,
                :controller_class_nesting,
                :controller_class_nesting_depth,
                :controller_class_name,
                :controller_singular_name,
                :controller_plural_name
  alias_method  :controller_file_name,  :controller_singular_name
  alias_method  :controller_table_name, :controller_plural_name

  def initialize(runtime_args, runtime_options = {})
    super
   
    # Take controller name from the next argument.  Default to the pluralized model name.
    @controller_name = runtime_args[0] #args.shift
    @controller_name ||= ActiveRecord::Base.pluralize_table_names ? @name.pluralize : @name

    base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
    @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)

    if @controller_class_nesting.empty?
      @controller_class_name = @controller_class_name_without_nesting
    else
      @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
    end
  end

  def manifest
    record do |m|
      
       m.class_collisions controller_class_path, "#{controller_class_name}Controller", 
                                                #"#{controller_class_name}ControllerTest", 
                                                "#{controller_class_name}Helper"

       m.class_collisions class_path,            "#{class_name}"
   
#      # check for class naming collisions
#      m.class_collisions controller_class_path, "#{controller_class_name}Controller"
#      # create directory
#      m.directory File.join('app/controllers', controller_class_path)
#      # copy from templates/ to our controller directory
#      m.template 'controller_template.rb', 
#      File.join('app/models', class_path, "#{file_name}.rb")


       m.directory File.join('app/models', class_path)
       m.directory File.join('app/controllers', controller_class_path)
       m.directory File.join('app/helpers', controller_class_path)
       m.directory File.join('app/views', controller_class_path, controller_file_name)
       
#       m.template 'models/article.rb',
#                  File.join('app/models',
#                            class_path,
#                            "#{file_name}.rb")
       
#        m.template 'models/branch.rb',
#                  File.join('app/models',
#                            class_path,
#                            "#{args[0]}.rb")  
       
       
#       m.template 'models/leaf.rb',
#                  File.join('app/models',
#                            class_path,
#                            "#{args[1]}.rb")
                            
                          

       m.template 'controllers/controller.rb',
                  File.join('app/controllers',
                            controller_class_path,
                            "#{controller_file_name}_controller.rb")
       m.template 'helpers/helper.rb',
                  File.join('app/helpers',
                            controller_class_path,
                            "#{controller_file_name}_helper.rb")
       
                   
       
#       m.file "models/article.rb","app/models/article.rb"
#       m.file "models/category.rb","app/models/category.rb"
#       m.file "controllers/#{controller_file_name}_controller.rb","app/controllers/#{controller_file_name}_controller.rb"
#       m.file "helpers/#{controller_file_name}_helper.rb","app/helpers/#{controller_file_name}_helper.rb"
#       m.directory "app/views/#{controller_file_name}"
#       m.file "views/_show_PPtree.rhtml", "app/views/#{controller_file_name}/_show_PPtree.rhtml"
#       m.migration_template "migrate/create_categories.rb", "db/migrate"
      
#       m.template "views/_show_PPtree.rhtml",
#                   File.join('app/views',
#                              'account',
#                              "_show_PPtree.rhtml")

      m.file "views/_show_ajax_tree.rhtml", "app/views/#{controller_file_name}/_show_ajax_tree.rhtml"
      
      #unless options[:skip_migration]
      #  m.migration_template 'migrate/migration.rb', 'db/migrate', :assigns => {
      #    :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"
      #  }, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
      #end
   
    end
  end 

 protected
    # Override with your own usage banner.
    def banner
      "Usage: #{$0} authenticated ModelName [ControllerName]"
    end

#  def file_name
#    "create_categories"
#  end
 

end
